<?php

/**
 * @author nhatnam
 * @copyright 2011
 */

include('clsketnoi.php');

function taomachitietphieuthanhly() 
{ 
    return taomatudong("select mactptls from tblchitietphieuthanhlysach", "mactptls", 5, 15);
} 

function themchitietptls()
{
    $mactptl = $_POST['mactptls'];
    $macuonsach = $_POST['macuonsach'];
    $maptl = $_POST['maptl'];
    $lydo = $_POST['lydo'];
    
    $query = mysql_query("insert into tblchitietphieuthanhlysach (mactptls, lydo, macuonsach, maptls) values ('$mactptl','$lydo','$macuonsach','$maptl')", dbketnoi());
    mysql_free_result($query);
}

function xoachitietptls()
{
    $mactptl = $_POST['mactptls'];
    $query = mysql_query("delete from tblchitietphieuthanhlysach where mactptls = '$mactptl'", dbketnoi());
    mysql_free_result($query);
}

function updatechitietptls()
{
    $mactptl = $_POST['mactptls'];
    $macuonsach = $_POST['macuonsach'];
    $maptl = $_POST['maptl'];
    $lydo = $_POST['lydo'];
    
    $query = mysql_query("update tblchitietphieuthanhlysach set lydo='$lydo', macuonsach='$macuonsach', maptls='$maptl' where mactptls='$mactptl')", dbketnoi());
    mysql_free_result($query);
}

function hienthichitietphieuthanhlysach()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT mactptls, macuonsach, lydo, maptls from tblchitietphieuthanhlysach",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodienchitietphieuthanhlysach.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotablechitietphieuthanhly();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='' >".$result['mactptls']."</a></td>";
            echo "<td>".$result['macuonsach']."</td>";
            echo "<td>".$result['lydo']."</td>";
            echo "<td>".$result['maptls']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotablechitietphieuthanhly()
{
    $title = array("Mã CTPTLS ","Mã Cuốn Sách", "Lý Do", "Mã PTLS");
    echo "<table style='border-color:black;'  cellspacing='1' width='670'>";
    echo "<tr>";
    for($i=0; $i<=3; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

?>